package com.software.project.service;

import com.software.project.entity.StudyTimeRecord;
import com.software.project.entity.UserInfo;
import com.software.project.repository.StudyTimeRecordRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Slf4j
public class StudyTimeRecordServiceImpl implements StudyTimeRecordService{

    @Autowired
    private StudyTimeRecordRepository studyTimeRecordRepository;
    @Autowired
    private UserInfoService userInfoService;

    @Override
    public double createRecord(StudyTimeRecord studyTimeRecord) {
        log.debug("studyTimeRecord: {}", studyTimeRecord);
        // 获取新增的智力值 （智力值兑换规则：1.0小时兑换1智力）
        double intelligence = studyTimeRecord.getStudyTime();
        // 获取相应的UserInfo对象
        UserInfo userInfo = userInfoService.findUserInfoByUserId(studyTimeRecord.getUserId());
        // 重新设置智力值
        userInfo.setIntelligence(userInfo.getIntelligence() + intelligence);
        // 重新设置综合素质
        userInfo.setComprehensive(userInfo.getIntelligence()*0.5 + userInfo.getPower()*0.3 + userInfo.getCharm()*0.2);
        // 重新设置今日学习时长
        userInfo.setTodayLearningTime(userInfo.getTodayLearningTime() + intelligence);
        // 更新用户的智力值、综合素质、今日学习时长
        userInfoService.createUserInfo(userInfo);
        // 更新用户学习时长
        studyTimeRecord.setStudyTime(userInfo.getTodayLearningTime());
        StudyTimeRecord save = studyTimeRecordRepository.save(studyTimeRecord);
        log.debug("intelligence: {}", intelligence);
        return intelligence;
    }

    @Override
    public StudyTimeRecord[] findRecord(String userId) {
        log.debug("userId: {}", userId);
        long timestamp = System.currentTimeMillis();
        Date end = new Date(timestamp - 86400000);
        Date start = new Date(timestamp - 604800000);
        List<StudyTimeRecord> studyTimeRecordList = studyTimeRecordRepository.findByUserIdAndStudyDateBetween(userId, start, end);
        StudyTimeRecord[] records = new StudyTimeRecord[7];
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < 7; i++) {
            long time = timestamp - (i + 1) * 86400000;
            Date date = new Date(time);
            boolean flag = true;
            for (StudyTimeRecord record : studyTimeRecordList) {
                if (format.format(record.getStudyDate()).equals(format.format(date))) {
                    flag = false;
                    records[i] = record;
                    break;
                }
            }
            if (flag) {
                records[i] = new StudyTimeRecord();
                records[i].setUserDateId(userId + format.format(date));
                records[i].setUserId(userId);
                records[i].setStudyTime(0);
                records[i].setStudyDate(date);
            }
            log.debug("records[" + i + "]: {}", records[i]);
        }
        return records;
    }

}
